/* 
    Purpose: Import all cleaned surveys, keep relevant variables,
    		 and append all datasets together.

    Creates: 1_PooledData.dta
*/

clear all
set more off
set maxvar 10000

cd "$Mydirectory1/"

*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

********************
**** ANES 
********************

	use "./1_DataSources/ANES/output/ANES56to70_analysis.dta", clear
	gen data="anes"
	label var data "Dataset"
	
	#delimit ;
	keep id_anes lnfaminc fam_inc fam_inc_real topcoded bottomcoded 
		 fatherfarm hs_ed coll_ed employed unionR *_dm year age* decade 
		 sex race black weight data moved_region married never_married 
		 widowed divorced separated foreignborn bornsouth fatheroccej 
		 motheroccej headofhh* *_notworking occRej eduR yrsschool* region4* 
		 dob fatherforeign bpl R_* flag*   ;
	#delimit cr	  
		 	
	replace occRej =. if occRej>81 
	
	tempfile anes
	save `anes'
	
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

********************
**** OCG 1962 
********************

	use "./1_DataSources/CPS_1962_1973/output/OCG62_IGEanalysis.dta", clear
	gen data="ocg62"
	
	#delimit ;
	keep id_* lnfaminc fam_inc fam_inc_real 
		 year age* decade sex race black *_dm 
		 foreignborn weight_* data employed 
		 bornsouth fatheroccej motheroccej 
		 headofhh* *_notworking occRej married 
		 never_married divorced widowed separated 
		 yrsschool* region4* dob bpl moved_region
		 fatherforeign topcoded bottomcoded edu_dad 
		 edu_dad_bin dad_hs_ed dad_coll_ed eduR 
		 fatherfarm hs_ed coll_ed R_* flag_* ;
	#delimit cr	  
	
	tempfile ocg62
	save `ocg62'
	
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

********************
**** OCG 1973 
********************	
	
	use "./1_DataSources/CPS_1962_1973/output/OCG73_IGEanalysis.dta", clear
	gen data="ocg73"
	gen sex=1
	
	#delimit ;
	keep id_* lnfaminc fam_inc fam_inc_real fatherfarm 
		 hs_ed coll_ed employed veteran unionR year sex 
		 age* decade race black *_dm foreignborn weight_* 
		 data bornsouth fatheroccej motheroccej headofhh* 
		 edu_dad *_notworking occRej married never_married 
		 separated eduR yrsschool* region4* dob fatherforeign 
		 topcoded bottomcoded bpl moved_region dad_hs_ed dad_coll_ed 
		 edu_dad_bin edu_mom mom_hs_ed mom_coll_ed edu_mom_bin 
		 R_* ;
	#delimit cr	  	 
	
	tempfile ocg73
	save `ocg73'
	
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

********************
**** NFS 1970 
********************

	use "./1_DataSources/FertilityStudy_1970/output/NFS70_analysis.dta", clear
	gen data="nfs"
	gen sex=2
	
	#delimit ;	
	keep id_* lnfaminc fam_inc fam_inc_real fatherfarm 
	     eduR hs_ed coll_ed employed occSPej region* moved_* 
	     year age* decade sex race black *_dm foreignborn weight* 
	     data married never_married widowed divorced separated 
	     bornsouth fatheroccej headofhh* *_notworking yrsschool
	     dob topcoded bottomcoded R_* flag_* ;
	#delimit cr

	drop moved_inout_south
	gen yrsschool = yrsschool_bin
	
	tempfile nfs
	save `nfs'
	
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

****************************
**** NLSMW (Mature Women)
****************************

	use "./1_DataSources/NLS_MatureWomen/output/NLSMW_analysis.dta", clear
	gen data="nlsmw"
	gen year=1967
	gen sex=2
	
	#delimit ;	
	keep id_* lnfaminc fam_inc fam_inc_real fatherfarm hs_ed coll_ed  
		 year age* decade sex race black *_dm foreignborn weight_* 
		 data married never_married widowed divorced separated 
		 bornsouth south_residence fatheroccej motheroccej headofhh* 
		 fatherforeign *_notworking eduR yrsschool* dob employed
		 topcoded bottomcoded  occSPej moved_region edu_dad* 
		 edu_mom* *_hs_ed *_coll_ed R_* ;
	#delimit cr
	
	tempfile nlsmw
	save `nlsmw'
	
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

*********************************
**** PSID: 1997 cross-section
*********************************

use ./1_DataSources/PSID97_CrossSection/output/psid1997_cleaned.dta, clear
	gen data = "psid1997"

	#delimit ;
	keep id_* lnfaminc fam_inc fam_inc_real year age* 
		 fatherfarm hs_ed coll_ed unionR decade sex 
		 race black foreignborn weight_* data married 
		 never_married widowed divorced separated veteran fatheroccej 
		 motheroccej *_notworking occRej yrsschool* region4* dob 
		 fatherforeign topcoded bottomcoded edu_dad* dad_hs_ed 
		 dad_coll_ed edu* mom* R_* sib* ;
	#delimit cr	  
	
	tempfile psid1997
	save `psid1997'
	
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

*********************************
**** PSID: 2017 cross-section
*********************************

use ./1_DataSources/PSID17_CrossSection/output/psid2017_cleaned.dta, clear
	gen data = "psid2017"

	#delimit ;
	keep id_* lnfaminc fam_inc fam_inc_real year age* 
		 fatherfarm hs_ed coll_ed unionR decade 
		 sex race black foreignborn weight_*
		 data married never_married widowed divorced separated 
		 veteran fatheroccej motheroccej *_notworking occRej 
		 yrsschool* region4* dob fatherforeign topcoded bottomcoded 
		 edu_dad* dad_hs_ed dad_coll_ed edu* mom* R_* sib* ;
	#delimit cr	  
	
	tempfile psid2017
	save `psid2017'
	
*****************************************************
**** NLSY79--(2002 xsection)
*****************************************************

	use "./1_DataSources/NLSY79/wrkdata/nlsy79_newxsec.dta", clear
	gen data="nlsy79"

	ren xweight_nlsy79 weight_nlsy79
	
	#delimit ;	
	keep hhid lnfaminc fam_inc_real weight_nlsy79 samp_id id_nlsy 
	     origid_nlsy fatherfarm topcoded *employed year age agesq 
	     decade sex  race black  foreignborn fatherforeign famid data  
		 bottomcoded married  hs_ed coll_ed fatheroccej motheroccej 
		 headofhh* *_notworking eduR yrsschool* dob occRej dad_coll_ed 
		 dad_hs_ed edu_dad* grewup_south lnfaminc fam_inc fam_inc_real 
		 year age* decade sex race black foreignborn fatherforeign  
		 data *employed   fatheroccej  married never_married widowed 
		 divorced separated yrsschool* dob topcoded* bottomcoded* occRej
		 edu* *hs_ed *coll_ed fam_inc unionR veteran region4 R_* sib* RELC* IDC*
		 ;
	#delimit cr
	
	tempfile nlsy79
	save `nlsy79'

*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

**********************************************
**** NLSYW68 (Younger Women)--1988 xsection
**********************************************

	use "./1_DataSources/NLSYW68/wrkdata/nlsyw68_newxsec.dta", clear
	gen data="nlsyw68"
	ren id id_nlsyw68 
	
	#delimit ;	
	keep id_* hhid lnfaminc fam_inc_real weight_nlsyw68 fatherfarm
		 year age* decade sex race black foreignborn 
		 fatherforeign data topcoded* bottomcoded* *hs_ed *coll_ed 
		 fatheroccej motheroccej headofhh* *_notworking eduR yrsschool* 
		 dob occRej dad_coll_ed dad_hs_ed edu_dad* grewup_south 
		 south_residence married never_married divorced widowed separated 
		 edu* fam_inc unionR veteran R_* sib_nlsyw68 idcode_* ;
	#delimit cr
	rename bottomcoded_son_ bottomcoded_son
	rename topcoded_son_ topcoded_son
	
	tempfile nlsyw68
	save `nlsyw68'
		
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

**********************************************
**** NLSYM66 (Younger Men)--1981 xsection
**********************************************

	use "./1_DataSources/NLSYM66/wrkdata/nlsym66_newxsec.dta", clear
	gen data= "nlsym66"
	ren id id_nlsym66
	
	#delimit ;
	keep id_* hhid lnfaminc fam_inc fam_inc_real year age* decade sex 
	     race black foreignborn fatherforeign weight_* data *employed 
	     grewup_south fatheroccej motheroccej headofhh* *_notworking married 
	     never_married widowed divorced separated yrsschool* dob topcoded* 
	     bottomcoded* occRej fatherfarm edu* *hs_ed *coll_ed unionR 
	     veteran south_residence R_* sib_nlsym66 idcode_* ; 
	#delimit cr	
	drop edu_73* 
	
	tempfile nlsym66
	save `nlsym66'

*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

********************
**** GSS
********************

	use "./1_DataSources/GSS/output/GSS_allyears_foranalysis.dta", clear
	gen data="gss"
	
	#delimit ;	
	keep id_* lnfaminc fam_inc fam_inc_real year age* fatherfarm hs_ed 
	     coll_ed employed unionR decade sex race black foreignborn 
	     weight_gss data married never_married widowed divorced separated 
	     veteran bornsouth fatheroccej motheroccej headofhh* *_notworking 
	     occRej eduR yrsschool* region4* dob fatherforeign reg16 topcoded 
	     bottomcoded moved_region edu_dad* dad_hs_ed dad_coll_ed edu* 
	     mom*  *hhsize* ;
	#delimit cr
		
	tempfile gss
	save `gss'
		
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

********************
**** AVTMH 1957
********************

	use "./1_DataSources/Americans_MentalHealth_1957/output/AVTMH1957_IGEanalysis.dta", clear
	gen data= "avtmh57"

	#delimit ;
		keep id_* lnfaminc fam_inc fam_inc_real fatherfarm year age* decade 
		     sex race black foreignborn fatherforeign weight_* data *employed 
		     bornsouth fatheroccej headofhh* *_notworking married never_married 
		     widowed divorced separated yrsschool* region4* dob topcoded* 
		     bottomcoded* occRej edu* *hs_ed *coll_ed *dm R_* flag_* ; 
	#delimit cr	  

	tempfile avtmh57
	save `avtmh57'

*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

********************
**** AVTMH 1976
********************

	use "./1_DataSources/Americans_MentalHealth_1976/output/AVTMH1976_cleaned.dta", clear
	gen data= "avtmh76"

	#delimit ;
		keep id_* lnfaminc fam_inc fam_inc_real fatherfarm year age* decade 
		     sex race black foreignborn weight_* data *employed bornsouth 
		     fatheroccej* motherocc* headofhh* *_notworking married 
		     never_married widowed divorced separated yrsschool* region4* 
		     dob bpl moved* *_childhood topcoded* bottomcoded* occRej 
		     edu* *hs_ed *coll_ed *dm R_* flag_* ; 
	#delimit cr	  

	tempfile avtmh76
	save `avtmh76'

*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

********************
**** NSFH
********************

	use "./1_DataSources/natl_survey_fam_HH/output/fam_hh_cleaned.dta", clear
	gen data="nsfh" 
	
	#delimit ;
		keep id_* lnfaminc fam_inc fam_inc_real fatherfarm year age* decade 
		     sex race black foreignborn weight_* data *employed bornsouth 
		     fatheroccej motheroccej headofhh* *_notworking married 
		     never_married widowed divorced separated yrsschool* region4* 
		     dob bpl moved* *_childhood place* topcoded* bottomcoded* occRej 
		     edu* *hs_ed *coll_ed veteran *dm R_* flag_* ; 
	#delimit cr
	drop R_dead* R_adopted*

	tempfile nsfh
	save `nsfh'

*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

********************
**** NSBA 1979-1980
********************

	use "./1_DataSources/NSBA/output/cleaned_nsba.dta", clear
	gen data="nsba" 
	
	#delimit ;
		keep id_* lnfaminc fam_inc fam_inc_real fatherfarm year age* decade 
		     sex race black foreignborn weight_nsba data *employed bornsouth 
		     fatheroccej motheroccej headofhh* *_notworking married 
		     never_married widowed divorced separated yrsschool* region4* 
		     dob bpl moved* *_childhood topcoded* bottomcoded* occRej 
		     edu* *hs_ed *coll_ed unionR *dm R_*  ; 
	#delimit cr	  

	tempfile nsba
	save `nsba'

*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

********************
**** NLS Older Men
********************

	use "./1_DataSources/NLS_older_men/output/nls_older_cleaned", clear
	gen data = "nlsom"
	
	#delimit ;
		keep id_* lnfaminc fam_inc fam_inc_real fatherfarm year age* decade 
		     sex race black foreignborn fatherforeign weight_*  data *employed
		     bornsouth south_residence fatheroccej motheroccej 
		     headofhh*  *_notworking married never_married divorced 
		     widowed separated yrsschool* dob moved* topcoded* bottomcoded* 
		     occRej edu* *hs_ed *coll_ed *dm R_* flag* ; 
	#delimit cr	  

*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*

********************
** APPEND ALL DATASETS
********************
	
	append using `anes'
	append using `ocg73'
	append using `ocg62'
	append using `psid1997'
	append using `psid2017'
	append using `nlsy79'
	append using `nlsyw68'
	append using `nlsym66'
	append using `nfs'
	append using `nlsmw'
	append using `gss'
	append using `avtmh57'
	append using `avtmh76'
	append using `nsfh'
	append using `nsba'	
	
*------------------------------------------------------------------------------*
*------------------------------------------------------------------------------*	

***************************************
** DROPPING UNNEEDED EXTRA VARIABLES **
***************************************
	
	#delimit ;
		drop age1-age5 agewedcr agerelex agerborn ageemp agekdbrn 
		     agecmeus aged agedpar agedchld 
		     south_dm unionH_dm 
		     urban_dm eduR_dm age_ccw agewed reg16 educop educ1 
		     educ2 educ3 educ4 educ5 educ30 educemp statename_childhood 
		     place_born place_grewup ageA ;
	#delimit cr

	label var hhid "HH ID for nlsy79, nlsym, and nlsyw"

	
********************
** SAVE DATASETS
********************

	compress 
	save ./3_Output/1_PooledData.dta, replace
